<?php
namespace app\common\model;

use think\Model;
use app\common\model\HuiYuanSupervise;
class StateChange extends Model
{
	protected $system;

	protected function initialize()
	{
		parent::initialize();
		$this->system = new SystemSet();
		$this->member_model= new HuiYuanSupervise();

	}
	/**
	 * 充值查阅与审核
	 * @param unknown $id
	 * @param unknown $cid
	 */
        public function cousultState($id,$cid){
		$czid=input('param.czid');
		$beizhu=input('param.beizhu');
		$where = array('id'=>$czid);
		$data = array('ydzt'=>1,'czadmin'=>session('user')['nickname'],'beizhu'=>$beizhu);
		$this->system->dUpdate('chongzhi_list',$where, $data);
		return $this->system->alertWin('操作成功', "/admin/recharge/cz_list_no?id=".$id."&cid=".$cid);
	}
	public function throughAudit(){
		StateChange::startTrans();
		$czid=input('czid');
		$status = input('statu');
		$where_a = array('a.id'=>$czid);
		$where=array('id'=>$czid);
		$beizhu=input('beizhu');
		
		$data = array('zt'=>$status,'czadmin'=>session('user')['nickname'],'shtime'=>time(),'beizhu'=>$beizhu);
		//查询相关充值数据
		try {
				$query = $this->CzListZhenSongBaiFeng($where_a,1);
				//判断赠送金额打码量是否开启
				$dml = StateChange::name('xt_dmsz')->where('id',1)->find();
					
				//判断单次充值是否满一定金额，达到赠送金额；
				$xtcz = StateChange::name('xt_chongzhi')->where('id',1)->find();
				$xtzj = 0;
				if($xtcz['dmopen'] == 1){
					$dmls=($query[0]['je']+$query[0]['hy_zsje']+$query[0]['hy_msje']+$query[0]['method_money'])*$dml['xxcz_dmbs'];
				}else{
					$dmls = $query[0]['je']*$dml['xxcz_dmbs'];
				}
					
				$user = StateChange::name('username')->where('username',$query[0]['username'])->find();
				$where_u = array('username'=>$user['username']);
				$savedata = array('je'=>$user['je']+$query[0]['je']+$query[0]['hy_zsje']+$query[0]['hy_msje']+$query[0]['method_money'],'dml'=>$user['dml']+$dmls,'czzje'=>$user['czzje']+$query[0]['je']);
			if($status=='1'){	
				$data['czhje'] = $savedata['je']; //充值后金额
				if(!$this->system->dUpdate('chongzhi_list',$where, $data)){
					throw new \Exception('操作失败');
				};
				if(!$this->SaveRecharge('username', $where_u, $savedata)){
					throw new \Exception('操作失败');
				};
				$czjes=$query[0]['hy_zsje']+$query[0]['hy_msje']+$query[0]['method_money'];
				StateChange::name('username')->where('username',$query[0]['username'])->setInc('cz_num');

				//满送金额，等级赠送金额，充值方案赠送金额
				$extcj = array(
						'username'=>$user['username'],
						'user_class'=>$user['us_class'],
						'fstime'=>time(),
						'czadmin' => '系统赠送',
				);
				$cjhje = floatval($query[0]['je']) + floatval($user['je']) + floatval($query[0]['hy_msje']);
				if( floatval($query[0]['hy_msje']) > 0 ){
					$extcj['beizhu'] = '系统赠送，充值满送金额';
					$extcj['cjje'] = $query[0]['hy_msje'];
					$extcj['cjhje'] = $cjhje;
					self::name('caijin_list')->insert($extcj);
				}
				$cjhje += floatval($query[0]['hy_zsje']);
				if( floatval($query[0]['hy_zsje']) > 0 ){
					$extcj['beizhu'] = '系统赠送，充值等级赠送金额';
					$extcj['cjje'] = $query[0]['hy_zsje'];
					$extcj['cjhje'] = $cjhje;
					self::name('caijin_list')->insert($extcj);
				}
				$cjhje += floatval($query[0]['method_money']);
				if( floatval($query[0]['method_money']) > 0 ){
					$extcj['beizhu'] = '系统赠送，充值方案赠送金额';
					$extcj['cjje'] = $query[0]['method_money'];
					$extcj['cjhje'] = $cjhje;
					self::name('caijin_list')->insert($extcj);
				}
				if(!$this->UserFaSong($query[0]['username'], "您账户充值金额：".$query[0]['je']."，赠送金额：".$czjes.".已充值到账，请注意查收！")){
					throw new \Exception('操作失败');
				}else{
					$this->member_model->caozuorizhi_xieru("审核了账户:".$query[0]['username']."充值".$query[0]['je']);
				}
				
			}elseif($status=='-1'){
				if(!$this->UserFaSong($query[0]['username'], "您的充值提交被退回，请注意查收！")){

					throw new \Exception('操作失败');
				}else{
					$this->member_model->caozuorizhi_xieru("拒绝了账户:".$query[0]['username']."充值".$query[0]['je']);
				}
				if(!$this->system->dUpdate('chongzhi_list',$where, $data)){
					throw new \Exception('操作失败');
				};
			}
			StateChange::commit();
		}catch (\Exception $e){
			StateChange::rollback();
			return $this->system->alertWin('操作失败', "/admin/recharge/cz_list_no");
		}
		return true;
		/* return $this->system->alertWin('操作成功', "/admin/recharge/cz_list_no"); */
	}

	/**
	 * 查询未审核充值
	 * @param string $where
	 * @param number $limit
	 * @param string $order
	 */
	public function PaniageLists($where='',$limit=10,$order=''){	
		return StateChange::name('chongzhi_list')->where($where)->order($order)->paginate($limit);
	}
	public function delSelectChongZhi($where='',$order=''){
		return StateChange::name('chongzhi_list')->where($where)->order($order)->select();
	}
	public function chongZhiDel($id){
		return StateChange::name('chongzhi_list')->where('id',$id)->delete();
	}
	/**
	 * 查询提现
	 */
	public function selectList($limit,$username,$time1,$time2){
		$a=$username;
		
		if($time1 && $time2){
		    $star = $time1;
		    $end = $time2;
		    $where['tixian.txtime']=['between',strtotime($star).','.strtotime($end)];
		}
		$where['tixian.zt']=['<',1];
		$where['tixian.nbzh']=['<',1];
		return StateChange::name('tixian_list')->alias('tixian')
					->field(['tixian.id as uuid','tixian.shtime as shtime','tixian.zt as zt','tixian.nbzh as nbzh','tixian.txje as txje','tixian.ifck as ifck','tixian.txtime as txtime','tixian.txip as txip','tixian.czadmin as czadmin','tixian.shadmin as shadmin'])
					->join('__USERNAME__ member','member.username = tixian.username')
					->field(['member.username as username','member.dml as dml','member.bankren as bankren','member.bankname as bankname','member.bankno as bankno'])
					->join('__US_CLASS__ usclass','usclass.id = member.us_class')
					->field(['usclass.class_name as class_name'])
					->where($where)->where('tixian.username','like','%'.$a.'%')->order('tixian.id desc')
					->paginate($limit,false,array('query'=>array('sDate1'=>input('sDate1'),'sDate2'=>input('sDate2'))));
	}
	/**
	 * 提现审核获取id
	 * @return unknown
	 */
	public function toExamine(){
		$czid=input('param.czid');
		$where_a = array('id'=>$czid);
		$admin_user=$_COOKIE['user'];
		$query = $this->system->dFind('tixian_list',$where_a);
		return $query['id'];
	}
	/**
	 * 拒绝提现返回
	 */
	public function tiXianReturn($txid=''){
		if($txid !='')
			$where=array('id'=>$txid);
			$data['zt']=2;
			$query = $this->system->dFind('tixian_list',$where);
			
			if($query){
				$this->member_model->caozuorizhi_xieru("退回账户:".$query['username']."提现".$query['txje']."的申请");
				echo $query['username'];
			}else{
				echo 'NO';
			}		
	}
/**
 * 提现列表
 * @param unknown $sql
 * @param unknown $field
 * @param unknown $where
 */
	public function txList($sql,$field,$where){	
		return StateChange::name($sql)->alias('tixian')
					->field($field)
					->join('__USERNAME__ member','member.username = tixian.username')
					->field(['member.username as username','member.bankren as bankren','member.bankname as bankname','member.bankno as bankno'])
					->join('__US_CLASS__ usclass','usclass.id = member.us_class')
					->field(['usclass.class_name as class_name'])
					->where($where)->order('tixian.id desc')
					->paginate(16);
	}
	/**
	 * 
	 * 彩金发放
	 * @param unknown $where
	 * @param unknown $field
	 */
	public function caiJinList($where,$field){
		return StateChange::name('caijin_list')->alias('cj')
					->field($field)
					->join('__US_CLASS__ uc','uc.id = cj.user_class')
					->field(['uc.class_name as class_name'])
					->where($where)->order('cj.id desc')
		            ->paginate(16);
	}
	/**
	 * 新增、修改数据
	 */
	public function InsertRecharge($sql,$data)
	{
		return StateChange::name($sql)->insert($data);
	}
	public function SaveRecharge($sql,$where,$data)
	{
		return StateChange::name($sql)->where($where)->update($data);
	}
	/**
	 * 给用户发送消息
	 * @param unknown $user
	 * @param String $test
	 */
	public function UserFaSong($user,$test){
		$data = array('username' =>$user,'test'=>$test,'xx_time'=>time(),'ifdq'=>0);
		return $this->InsertRecharge('xiaoxi', $data);
	}
	
	public function CzListZhenSongBaiFeng($where='',$limit){
		$res = StateChange::name('chongzhi_list')
					->alias('a')
					->field(['a.id as uid','a.username as username','a.hy_zsje as hy_zsje','a.hy_zsbs as hy_zsbs','a.hy_msje as hy_msje','a.hy_msbs as hy_msbs','a.ddbh as ddbh','a.je as je','a.nbzh as nbzh','a.cztime as cztime','a.czclass as czclass','a.liuyan as liuyan','a.czip as czip','a.zt as zt','a.xxbank as xxbank','a.czadmin as czadmin','a.method_money'])
					->join('__USERNAME__ b','a.username=b.username')
					->field(['b.us_class as us_class','b.dml as dml'])
					->where($where)
					->order('a.id desc')
					->paginate($limit,false,['query'=>request()->param()]);
		return $res;
	}
	
}